package org.example.myframework.frame.common.mybatisPlus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 元对象处理器接口：实现自动填充
 */

@Component
@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    public MybatisPlusMetaObjectHandler() {
        log.info("加载【persistence】mybaitsplus元对象处理...............................");
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        // todo 后续登录时获取用户id插入
        this.strictInsertFill(metaObject, "createUser", Long.class, 1L);
        // 逻辑删除位，默认0
        this.setFieldValByName("delFlag", "0", metaObject);
        // 乐观锁字段
        this.strictInsertFill(metaObject, "version", Integer.class, 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateUser", Long.class, 2L);
    }
}
